package com.dbManage.page.user;

import com.dbManage.common.Constant;
import com.dbManage.dao.UserDao;
import com.dbManage.entity.User;
import com.dbManage.exception.PasswordException;
import com.dbManage.exception.UserRoleException;
import com.dbManage.tools.SessionTool;
import com.wsse.common.tool.Console;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserService {
    @Resource
    UserDao userDao;

    /**
     * 修改用户权限
     * @param user
     * @throws UserRoleException
     */
    public void updateUserRole(User user) throws UserRoleException {
        User mUser= SessionTool.getUser();
        if (mUser.getRole()!= Constant.ROLE_SUPER_MANAGER){
            throw new UserRoleException("无权修改");
        }
        userDao.updateRole(user);
    }

    /**
     * 根据管理权限,获取用户列表
     * @return
     */
    public List<User> getUserList() {
        User mUser= SessionTool.getUser();
        List<User> list=null;
        if (mUser.getRole()== Constant.ROLE_SUPER_MANAGER)
            list=userDao.findAllUser();
        if (mUser.getRole()==Constant.ROLE_MANAGER)
            list=userDao.findUserByRole(Constant.ROLE_GENERAL);
        return list;
    }

    /**
     *  获取用户信息,非用户本身或管理员无法获取
     * @param id
     * @return
     * @throws UserRoleException
     */
    public User findUserById(int id) throws UserRoleException {
        int role= SessionTool.getUser().getRole();
        int mId= SessionTool.getUser().getId();
        if (role!= Constant.ROLE_SUPER_MANAGER&&role!=Constant.ROLE_MANAGER&&mId!=id){
            throw new UserRoleException("无权获取");
        }
        return userDao.findUserById(id);
    }

    /**
     * 修改账户信息,非用户本身或管理员无法修改
     * @param user
     */
    public void updateUser(User user) throws UserRoleException {
        int role= SessionTool.getUser().getRole();
        int mId= SessionTool.getUser().getId();
        if (role!= Constant.ROLE_SUPER_MANAGER&&role!=Constant.ROLE_MANAGER&&mId!=user.getId()){
            throw new UserRoleException("修改失败");
        }
        userDao.updateUserInfo(user);
    }

    /**
     * 删除用户信息
     * @param id
     */
    public void deleteUser(int id) throws UserRoleException {
        int role= SessionTool.getUser().getRole();
        if (role!= Constant.ROLE_SUPER_MANAGER&&role!=Constant.ROLE_MANAGER){
            throw new UserRoleException("删除失败");
        }
        userDao.deleteUser(id);
    }

    /**
     * 用户筛选
     * @param user
     * @return
     */
    public List<User> queryUser(User user) {
        if (SessionTool.getUser().getRole()==Constant.ROLE_MANAGER)
            user.setRole(3);
        return userDao.queryUser(user);
    }

    /**
     * 修改密码
     * @param password
     * @param oldPassword
     */
    public void updateUserPassword(String password, String oldPassword) throws PasswordException {
        User user=SessionTool.getUser();
        Console.log("password:"+user.getPassword());
        Console.log("old_password:"+oldPassword);
        if(!user.getPassword().equals(oldPassword))
            throw new PasswordException("原密码错误");
        user.setPassword(password);
        userDao.updatePassword(user);
    }
}
